//
//  ZZLightViewTool.m
//  LightView
//
//  Created by evan on 2017/10/13.
//  Copyright © 2017年 evan. All rights reserved.
//

#import "ZZLightViewTool.h"

@implementation ZZLightViewTool

#pragma mark - 流星
/**
 流行 向右 单条
 */
+ (CALayer *)toRightOnelightLayerForColor:(UIColor *)color layerFrame:(CGRect)frame tuoweiLength:(CGFloat )length {
    
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors =  @[(__bridge id)[UIColor clearColor].CGColor, (__bridge id)color.CGColor];
    gradientLayer.locations = @[@(length),@(1)];;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = frame;
    gradientLayer.cornerRadius = frame.size.height / 2;
    gradientLayer.masksToBounds = YES;
    
    return gradientLayer;
}

/**
 流行 向右 多条
 */
+ (CALayer *)toRightMorelightLayerForColor:(NSArray <UIColor *>*)colors layerFrame:(CGRect)frame tuoweiLength:(CGFloat )length {
    
    CALayer *layer = [[CALayer alloc] init];
    
    layer.frame = frame;
    CGFloat width = frame.size.width / colors.count;
    CGFloat height = frame.size.height;
    layer.cornerRadius = frame.size.height / 2;
    CGFloat x = 0;
    CGFloat y = 0;
    
    for (NSInteger index = 0; index< colors.count; index++) {
        
        UIColor *color = colors[index];
        x = width * index;
        CGRect frameLayer = CGRectMake(x, y, width, height);
        CALayer * subLayer = [ZZLightViewTool toRightOnelightLayerForColor:color layerFrame:frameLayer tuoweiLength: length];
        [layer addSublayer:subLayer];
    }
    
    return layer;
}

/**
 流行 向左 单条
 */
+ (CALayer *)toLeftlightLayerForColor:(UIColor *)color layerFrame:(CGRect)frame tuoweiLength:(CGFloat )length {
    
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors =  @[(__bridge id)color.CGColor, (__bridge id)[UIColor clearColor].CGColor];
    gradientLayer.locations = @[@(0),@( length)];;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = frame;
    gradientLayer.cornerRadius = frame.size.height / 2;
    gradientLayer.masksToBounds = YES;
    
    return gradientLayer;
}

/**
 流行 向左 多条
 */
+ (CALayer *)toLeftMorelightLayerForColor:(NSArray <UIColor *>*)colors layerFrame:(CGRect)frame tuoweiLength:(CGFloat )length {
    
    CALayer *layer = [[CALayer alloc] init];
    
    layer.frame = frame;
    layer.cornerRadius = frame.size.height / 2;
    CGFloat width = frame.size.width / colors.count;
    CGFloat height = frame.size.height;
    CGFloat x = 0;
    CGFloat y = 0;
    
    for (NSInteger index = 0; index< colors.count; index++) {
        
        UIColor *color = colors[index];
        x = width * index;
        CGRect frameLayer = CGRectMake(x, y, width, height);
        CALayer * subLayer = [ZZLightViewTool toLeftlightLayerForColor:color layerFrame:frameLayer tuoweiLength:length];
        [layer addSublayer:subLayer];
    }
    
    return layer;
}

+ (CALayer *)liuXinglightLayerForColors:(NSArray<UIColor *>*)colors layerFrame:(CGRect)frame tuoweiLength:(CGFloat )length LightType:(LightLineDirectionModel)type {
    
    CALayer *layer = nil;
    
    switch (type) {
        case LightLineDirectionLeft:
        {
             layer = [ZZLightViewTool toRightMorelightLayerForColor:colors layerFrame:frame tuoweiLength:length];
        }
            break;
        case LightLineDirectionRight:
        {
             layer = [ZZLightViewTool toLeftMorelightLayerForColor:colors layerFrame:frame tuoweiLength:1 - length];
        }
            break;
            
        case LightLineDirectionCenter:
        {
            layer = [[CALayer alloc] init];
            layer.frame = frame;
            
            layer.cornerRadius = frame.size.height / 2;
            CALayer *layerLeft = [ZZLightViewTool toRightMorelightLayerForColor:colors layerFrame:CGRectMake(0, 0, frame.size.width / 2 - 10, frame.size.height) tuoweiLength:length];
            
            NSMutableArray *colorsRight = [NSMutableArray array];
            for (NSInteger index = 0; index< colors.count; index++) {
                
                [colorsRight addObject:colors[colors.count - 1 - index]];
            }
            
            CALayer *layerRight = [ZZLightViewTool toLeftMorelightLayerForColor:colorsRight layerFrame:CGRectMake(frame.size.width / 2 + 10, 0, frame.size.width / 2 - 10, frame.size.height) tuoweiLength:1 -length];
            
            [layer addSublayer:layerLeft];
            [layerLeft addSublayer:layerRight];
        }
            break;
            
        default:
            break;
    }
    
    return layer;
}

#pragma mark - 渐变
/**
 渐变 向左 多条
 */
+ (CALayer *)jianBianToLeftMorelightLayerForColor:(NSArray <UIColor *>*)colors layerFrame:(CGRect)frame {
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    
    NSMutableArray *colorsMtb = [NSMutableArray array];
    for (UIColor *color in colors) {
        
        [colorsMtb addObject: (__bridge id)color.CGColor];
    }
    
    gradientLayer.colors = colorsMtb;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = frame;
    gradientLayer.cornerRadius = frame.size.height / 2;
    gradientLayer.masksToBounds = YES;
    
    return gradientLayer;
}

+ (CALayer *)jianBianlightLayerForColors:(NSArray<UIColor *>*)colors layerFrame:(CGRect)frame LightType:(LightLineDirectionModel)type {
    
    CALayer *layer = nil;
    switch (type) {
        case LightLineDirectionLeft:
        {
            layer = [ZZLightViewTool jianBianToLeftMorelightLayerForColor:colors layerFrame:frame];
        }
            break;
        case LightLineDirectionRight:
        {
            NSMutableArray *colorsRight = [NSMutableArray array];
            for (NSInteger index = 0; index < colors.count; index++) {
                
                [colorsRight addObject:colors[colors.count - 1 - index]];
            }
            layer = [ZZLightViewTool jianBianToLeftMorelightLayerForColor:colorsRight layerFrame:frame];
        }
            break;
        default:
            break;
    }
    
    return layer;
}

#pragma mark - 覆盖
+ (CALayer *)fugailightToLeftColors:(NSArray<UIColor *>*)colors layerFrame:(CGRect)frame LightType:(LightLineDirectionModel)type {
    
    CALayer *layer = [CALayer layer];
    layer.frame = frame;
    layer.cornerRadius = frame.size.height / 2;
    
    CGFloat layerX = 0;
    CGFloat layerY = 0;
    CGFloat layerW = frame.size.width / colors.count;
    CGFloat layerH = frame.size.height;
    
    
    for (NSInteger index = 0; index < colors.count; index ++) {
        
        CALayer *subLayer = [CALayer layer];
        subLayer.frame = CGRectMake(layerX + layerW * index, layerY, frame.size.width - layerW * (index), layerH);
        subLayer.backgroundColor = colors[colors.count - 1 - index].CGColor;
        subLayer.cornerRadius = frame.size.height / 2;
        [layer addSublayer:subLayer];
    }
    
    return layer;
}

+ (CALayer *)fugailightToRightColors:(NSArray<UIColor *>*)colors layerFrame:(CGRect)frame LightType:(LightLineDirectionModel)type {
    
    CALayer *layer = [CALayer layer];
    layer.frame = frame;
    layer.cornerRadius = frame.size.height / 2;
    
    CGFloat layerX = 0;
    CGFloat layerY = 0;
    CGFloat layerW = frame.size.width / colors.count;
    CGFloat layerH = frame.size.height;
    
    
    for (NSInteger index = 0; index < colors.count; index ++) {
        
        CALayer *subLayer = [CALayer layer];
        subLayer.frame = CGRectMake(layerX, layerY, frame.size.width - layerW * (index), layerH);
        subLayer.backgroundColor = colors[colors.count - 1 - index].CGColor;
        subLayer.cornerRadius = frame.size.height / 2;
        [layer addSublayer:subLayer];
    }
    
    return layer;
}

+ (CALayer *)fugailightLayerForColors:(NSArray<UIColor *>*)colors layerFrame:(CGRect)frame LightType:(LightLineDirectionModel)type {
    
    CALayer *layer = [CALayer layer];
    
    switch (type) {
        case LightLineDirectionLeft:
        {
            layer = [ZZLightViewTool fugailightToLeftColors:colors layerFrame:frame LightType:type];
        }
            break;
        case LightLineDirectionRight:
        {
            layer = [ZZLightViewTool fugailightToRightColors:colors layerFrame:frame LightType:type];
        }
            break;
        case LightLineDirectionCenter:
        {
        }
            break;
            
        default:
            break;
    }
    
    return layer;
}

#pragma mark - 间隔
+ (CALayer *)JianGeLayerForColors:(NSArray<UIColor *>*)colors layerFrame:(CGRect)frame LightType:(LightLineDirectionModel)type {
    
    CALayer *layer = nil;
    switch (type) {
        case LightLineDirectionLeft:
        {
            layer = [ZZLightViewTool JianGeToLeftForColors:colors layerFrame:frame LightType:type];
        }
            break;
        case LightLineDirectionRight :{
            
            layer = [ZZLightViewTool JianGeToRightForColors:colors layerFrame:frame LightType:type];
        };
            break;
        case LightLineDirectionCenter: {
            
            layer = [ZZLightViewTool JianGeToRightForColors:colors layerFrame:frame LightType:type];
        }
        default:
            break;
    }
    
    
    return layer;
}

+ (CALayer *)JianGeToRightForColors:(NSArray<UIColor *>*)colors layerFrame:(CGRect)frame LightType:(LightLineDirectionModel)type {
    
    CALayer *layer = [CALayer layer];
    layer.frame = frame;
    CGFloat jW = 100;
    CGFloat jH = 10;
    CGFloat JX = (frame.size.width - jW) / 2.0;
    CGFloat JY = 0;
    CALayer *jianG = [CALayer layer];
    jianG.backgroundColor = [UIColor greenColor].CGColor;
    jianG.frame = CGRectMake(JX, JY, jW, jH);
    jianG.cornerRadius = 5;
    [layer addSublayer:jianG];
    
    CGFloat subLayerW = (frame.size.width - jW) / colors.count / 2;
    CGFloat subLayerH = 10;
    CGFloat subLayerX = 0;
    CGFloat subLayerY = 0;
    
    for (NSInteger index = 0; index < colors.count; index++) {
        
        CALayer *subLayer = [CALayer layer];
        subLayer.backgroundColor = colors[index].CGColor;
        
        subLayer.frame = CGRectMake(index * subLayerW, subLayerY, subLayerW + 10, subLayerH);
        subLayer.cornerRadius = 5;
        
        [layer addSublayer:subLayer];
    }
    
    for (NSInteger index = 0; index < colors.count; index++) {
        
        CALayer *subLayer = [CALayer layer];
        subLayer.backgroundColor = colors[index].CGColor;
        
        subLayer.frame = CGRectMake(frame.size.width - (index+1) * subLayerW - 10, subLayerY, subLayerW + 10, subLayerH);
        subLayer.cornerRadius = 5;
        
        [layer addSublayer:subLayer];
        
    }
    
    return layer;
}

+ (CALayer *)JianGeToLeftForColors:(NSArray<UIColor *>*)colors layerFrame:(CGRect)frame LightType:(LightLineDirectionModel)type {
    
    CALayer *layer = [CALayer layer];
    layer.frame = frame;
    CGFloat jW = 100;
    CGFloat jH = 10;
    CGFloat JX = (frame.size.width - jW) / 2.0;
    CGFloat JY = 0;
    CALayer *jianG = [CALayer layer];
    jianG.backgroundColor = [UIColor greenColor].CGColor;
    jianG.frame = CGRectMake(JX, JY, jW, jH);
    jianG.cornerRadius = 5;
    [layer addSublayer:jianG];
    
    CGFloat subLayerW = (frame.size.width - jW) / colors.count / 2;
    CGFloat subLayerH = 10;
    CGFloat subLayerX = 0;
    CGFloat subLayerY = 0;
    
    for (NSInteger index = 0; index < colors.count; index++) {
        
        CALayer *subLayer = [CALayer layer];
        subLayer.backgroundColor = colors[colors.count - 1 - index].CGColor;
        
        subLayer.frame = CGRectMake( (frame.size.width - jW) / 2 - (1 + index) * subLayerW, subLayerY, subLayerW + 10, subLayerH);
        subLayer.cornerRadius = 5;
        
        [layer addSublayer:subLayer];
    }
    
    for (NSInteger index = 0; index < colors.count; index++) {
        
        CALayer *subLayer = [CALayer layer];
        subLayer.backgroundColor = colors[colors.count - 1 - index].CGColor;
        
        
        subLayer.frame = CGRectMake((frame.size.width - jW) / 2 + jW + (index) * subLayerW - 10, subLayerY, subLayerW + 10, subLayerH);
        subLayer.cornerRadius = 5;
        
        [layer addSublayer:subLayer];
        
    }
    
    return layer;
}

@end

